/***************************************************
 @Name :        小铭同学
 @Time :        2025-09-03
 @Email :       LiaoMingWJ233@163.com
 @Comment :     视频播放器
 @FileName :    widget.cpp
 @Version :     1.0
****************************************************/


#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    
    ui->exit->raise();
    PushButton_music(ui->exit);
    
    ui->label_bj->setPixmap(QPixmap(":/主题背景"));
        
    ui->label_zt->setText("视频播放器");
    ui->label_zt->setStyleSheet("QLabel {"
        "    font-size: 50px;"           // 字体大小
        "    color: #1837ae;"            // 文字颜色
        "    font-weight: bold;"         // 字体粗细
        "    font-family: '楷体';"        // 字体家族
        "}"
    );
       
    ui->pushButton_suspend->setIcon(QIcon(":/暂停_开"));
    ui->pushButton_suspend->setText("");
    ui->pushButton_suspend->setIconSize(QSize(75, 60));
    ui->pushButton_suspend->setFlat(true);  // 移除默认背景（可选，但可能影响 hover 效果）
    ui->pushButton_suspend->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
    
    ui->pushButton_play->setIcon(QIcon(":/播放_开"));
    ui->pushButton_play->setText("");
    ui->pushButton_play->setIconSize(QSize(75, 60));
    ui->pushButton_play->setFlat(true);  // 移除默认背景（可选，但可能影响 hover 效果）
    ui->pushButton_play->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
    
    
    // 视频播放
    // 创建媒体播放器和视频显示部件
    mediaPlayer = new QMediaPlayer(this);
    
    // 当播放结束时重新播放
    connect(mediaPlayer, &QMediaPlayer::mediaStatusChanged, [=](QMediaPlayer::MediaStatus status) {
        if (status == QMediaPlayer::EndOfMedia) {
            mediaPlayer->setPosition(0);  // 回到开头
            mediaPlayer->play();          // 重新播放
        }
    });
    
    // 创建图形场景和视图
    QGraphicsScene *scene = new QGraphicsScene(this);
    QGraphicsVideoItem *videoItem = new QGraphicsVideoItem;
    scene->addItem(videoItem);
    
    // 设置视频输出到QVideoWidget
    mediaPlayer->setVideoOutput(videoItem);
    
    // 创建图形视图并设置场景
    QGraphicsView *graphicsView = new QGraphicsView(this);
    graphicsView->setScene(scene);
    graphicsView->setGeometry(0,0,600,300);
    
    // 设置无边框
    graphicsView->setFrameShape(QFrame::NoFrame);
    graphicsView->setStyleSheet("background: transparent; border: 0px;");
    
    // 控制视频窗口属性
    graphicsView->setBackgroundBrush(Qt::NoBrush);        // 透明背景
    graphicsView->setRenderHint(QPainter::Antialiasing);  // 抗锯齿
    graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);  // 隐藏水平滚动条
    graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    // 隐藏垂直滚动条
    
    // 调整视频项尺寸适应视图
    videoItem->setSize(graphicsView->size());
    
    // 使用布局管理器将 videoWidget 添加到主窗口
    QVBoxLayout *layout = new QVBoxLayout(ui->label);
    layout->setContentsMargins(0, 0, 0, 0);  // 移除边距，让视频填满整个 label
    layout->addWidget(graphicsView);
    ui->label->setLayout(layout);    // 应用布局   
    
    // 设置媒体内容
    mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
    qDebug()<<"已加载: " + fileName << endl;
    
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_play_clicked()
{
    ui->pushButton_suspend->setIcon(QIcon(":/暂停_关"));
    ui->pushButton_play->setIcon(QIcon(":/播放_开"));
    mediaPlayer->play();
    //qDebug()<<"视频播放中..."<<endl;
}

void Widget::on_pushButton_suspend_clicked()
{
    ui->pushButton_play->setIcon(QIcon(":/播放_关"));
    ui->pushButton_suspend->setIcon(QIcon(":/暂停_开"));
    mediaPlayer->pause();
    //qDebug()<<"视频已暂停"<<endl;
}

void Widget::PushButton_music(QPushButton* PushButton)
{
    PushButton->setStyleSheet(R"(
        QPushButton {
            background-color: #d0dba8;
            color: white;
            border-radius: 15px;
            border: none;
            padding: 4px 4px;
            font-size: 20px;
            font-weight: bold;
            font-family: "楷体";
        }
        QPushButton:hover {
            background-color: #3367d6;
        }
        QPushButton:pressed {
            background-color: #2a56c6;
        }
    )");
}


void Widget::on_exit_clicked()
{
    this->close();
    QApplication::quit();
}

